package ar.edu.unq.mongodbexample.tests.persitence;

import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;
import ar.edu.unq.mongodbexample.homes.CareerHome;
import ar.edu.unq.mongodbexample.homes.ProfessorHome;
import ar.edu.unq.mongodbexample.homes.StudentHome;
import ar.edu.unq.mongodbexample.homes.UniversityHome;
import ar.edu.unq.mongodbexample.model.Career;
import ar.edu.unq.mongodbexample.model.Professor;
import ar.edu.unq.mongodbexample.model.Student;
import ar.edu.unq.mongodbexample.model.Univertity;
import ar.edu.unq.mongodbexample.persistence.MongoDBManager;

/**
 * TODO: description
 */
public class UniversityHomeTestCase extends TestCase {

    UniversityHome home;

    List<Univertity> universities;

    @Override
    protected void setUp() throws Exception {
        this.home = new UniversityHome();
        this.universities = this.createUniversities();
    }

    @Override
    protected void tearDown() throws Exception {
        MongoDBManager.getInstance().getDataStore().getCollection(Univertity.class).drop();
        MongoDBManager.getInstance().getDataStore().getCollection(Professor.class).drop();
        MongoDBManager.getInstance().getDataStore().getCollection(Student.class).drop();
        MongoDBManager.getInstance().getDataStore().getCollection(Career.class).drop();
    }

    public void testSaveAndGetUniversityByName() {
        Univertity university = new Univertity("UNQ",null,null,null);
        this.home.save(university);
        Univertity foundUniversity = this.home.findById("UNQ");
        assertEquals(foundUniversity.getName(), "UNQ");
    }
    public void testSaveAndGetUniversityByNameAndGetReferences() {
    	StudentHome studentHome=new StudentHome();
    	ProfessorHome professorHome=new ProfessorHome();
    	CareerHome careerHome=new CareerHome();
    	
    	studentHome.saveAll(getStudents());
    	professorHome.saveAll(getProfessors());
    	careerHome.saveAll(getCareers());
    	
        Univertity university = new Univertity("UNQ",getCareers(),getProfessors(),getStudents());
        this.home.save(university);
        Univertity foundUniversity = this.home.findById("UNQ");
        
        assertEquals(foundUniversity.getName(), "UNQ");
        assertEquals(4, foundUniversity.getCareers().size());
        assertEquals(4, foundUniversity.getProfessors().size());
        assertEquals(4, foundUniversity.getStudents().size());
    }

    public void testSaveAllAndGetAllUniversities() {
    	List<Univertity> universities = new ArrayList<Univertity>();
        universities.add(new Univertity("UNQ",null,null,null));
        universities.add(new Univertity("UNLP",null,null,null));
        universities.add(new Univertity("UBA",null,null,null));

        this.home.saveAll(universities);
        List<Univertity> universityList = this.home.getAll();
        assertEquals(universityList.size(), 3);
    }

    private List<Univertity> createUniversities() {
        List<Univertity> universities = new ArrayList<Univertity>();
        universities.add(new Univertity("UNQ",getCareers(),getProfessors(),getStudents()));
        universities.add(new Univertity("UNLP",getCareers(),getProfessors(),getStudents()));
        universities.add(new Univertity("UBA",getCareers(),getProfessors(),getStudents()));
        return universities;
    }
    
    private List<Student> getStudents(){
    	List<Student> students = new ArrayList<Student>();
    	students.add(new Student(00000001, 19523, "UnApellido1", "UnNombre1","unemailmorphia1@mail.com"));
    	students.add(new Student(00000002, 29523, "UnApellido2", "UnNombre2","unemailmorphia1@mail.com"));
    	students.add(new Student(00000003, 39523, "UnApellido3", "UnNombre3","unemailmorphia1@mail.com"));
    	students.add(new Student(00000004, 49523, "UnApellido4", "UnNombre4","unemailmorphia1@mail.com"));
        return students;
    }
    
    public List<Professor> getProfessors(){
		List<Professor> professorsList=new ArrayList<Professor>();
		professorsList.add(new Professor(00000001,"UnApellido1","UnNombre1","unemailmorphia1@mail.com"));
		professorsList.add(new Professor(00000002,"UnApellido2","UnNombre2","unemailmorphia2@mail.com"));
		professorsList.add(new Professor(00000003,"UnApellido3","UnNombre3","unemailmorphia3@mail.com"));
		professorsList.add(new Professor(00000004,"UnApellido4","UnNombre4","unemailmorphia4@mail.com"));
        return professorsList;
	}
    
    public List<Career> getCareers(){
		List<Career> careerList=new ArrayList<Career>();
		careerList.add(new Career(1,"Tecnicatura en Programacion Informatica"));
		careerList.add(new Career(2,"Terapia Ocupacional"));
		careerList.add(new Career(3,"Ingenieria Naval"));
		careerList.add(new Career(4,"Licenciatura en Biotecnologia"));
        return careerList;
	}
}
